#!/bin/bash
#
# Copyright (C) 2017 ~ 2018 Lingmo Technology Co., Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# Copy boot folder to loongson system.

if ! is_loongson; then
    return 0
fi

TARGET=/target/boot
boot_path=

# Remove overlay filesystem item.
sed -i "/overlay/d" /target/etc/fstab

DI_ROOT_PARTITION=$(cat /target/etc/lingmo-installer.conf | \
  grep "^DI_ROOT_PARTITION=" | awk -F'=' '{print $2}')
rootuuid=UUID=$(blkid -s UUID -o value ${DI_ROOT_PARTITION})
bootuuid=$(cat /etc/fstab |grep "/boot" |awk '{print $1}')
if [ "x$bootuuid" = "x" ];then
	bootuuid=$rootuuid
fi

if [ x$(installer_get "DI_CRYPT_ROOT") != xtrue ]; then
    splash="splash"
fi

DI_LOOP_ROOT_FILE=$(installer_get "DI_LOOP_ROOT_FILE")
echo "ctx ---------  DI_LOOP_ROOT_FILE=$DI_LOOP_ROOT_FILE"

# Default, at the same moment, both of boot.cfg and grub.cfg exist.
if [ -f $TARGET/boot.cfg ];then
	boot_path=$TARGET
elif [ -f $TARGET/boot/boot.cfg ];then
	boot_path=$TARGET/boot
fi

#sda1已格式化
if [ ! $boot_path ];then
	# Copy boot folder and follow symbolic link.
	if [ -d /lib/live/mount/medium/live/boot ]; then
    	cp -rvP /lib/live/mount/medium/live/boot/* /target/boot/ || error "Failed to copy boot/"
    	cd /target/boot/ && ln -sf . boot && cd -
	else
        error "Not boot folder found!"
    fi

    [ -f $TARGET/grub.cfg ] && [ -f $TARGET/boot.cfg ] && echo "boot files copied!"

    if [ -d /lib/live/mount/medium/live/modules ]; then
	    mkdir -p /target/lib/modules || true
        cp -r /lib/live/mount/medium/live/modules/* /target/lib/modules/ || error "Failed to copy kernel modules"
    fi

    kernel_name=$(ls /target/boot/ |grep vmlinu|grep $(uname -r))
    initrd_name=$(ls /target/boot/ |grep initrd|grep $(uname -r))

    # modify boot.cfg & grub.cfg set boot.cfg args root=UUID=boot_path
    sed -i -e "s|root=|root=$rootuuid|g" -e "s|live-media=|live-media=$bootuuid|g" -e "s|kernel_name|$kernel_name|g" -e "s|initrd_name|$initrd_name|g" /target/boot/boot.cfg
    sed -i -e "s|root=|root=$rootuuid|g" -e "s|live-media=|live-media=$bootuuid|g" -e "s|kernel_name|$kernel_name|g" -e "s|initrd_name|$initrd_name|g" /target/boot/grub.cfg

    if [ x$(installer_get "DI_CRYPT_ROOT") == xtrue ];then
        sed -i -e 's/splash//g' /target/boot/grub.cfg
        sed -i -e 's/splash//g' /target/boot/boot.cfg
    fi

    if grep -q boot /target/etc/fstab ;then
        sed -i "s|wd0/boot|wd0|g" /target/boot/boot.cfg
    fi

    if [ -d $TARGET/EFI ];then
        cd $TARGET/EFI/BOOT
        ln -sf ../../grub.cfg grub.cfg
        ln -sf ../../$kernel_name $kernel_name
        ln -sf ../../$initrd_name $initrd_name
        ln -sf ../../lingmo lingmo
        cd -
    fi
#存在多个系统
else
    uuid=$(echo $rootuuid |cut -c 6-)
    echo "ctx uuid=$uuid"
    discid=$(sudo blkid |grep -i $uuid| awk '{print $1}'|cut -d '/' -f 3 |cut -d ':' -f 1)

    kernel_name=$(ls /lib/live/mount/medium/live/boot/ |grep vmlinu|grep $(uname -r))-$discid
    initrd_name=$(ls /lib/live/mount/medium/live/boot/ |grep initrd|grep $(uname -r))-$discid

    if [ -d /lib/live/mount/medium/live/boot ]; then
        for file in $(ls /lib/live/mount/medium/live/boot |grep $(uname -r));do
    	    cp -rvP /lib/live/mount/medium/live/boot/$file $boot_path/$file-$discid
            cp -rvf /lib/live/mount/medium/live/boot/lingmo/$file $boot_path/lingmo/$file-$discid
        done

    	cd $boot_path && ln -sf . boot && cd -
    fi

    if [ -d /lib/live/mount/medium/live/modules ]; then
	    mkdir -p /target/lib/modules || true
        cp -r /lib/live/mount/medium/live/modules/* /target/lib/modules/ || error "Failed to copy kernel modules"
    fi

    echo "
title Lingmo 15.5 SP3 Professional mips64el for Loongson 3A3000
    initrd /dev/fs/ext2@wd0/boot/$initrd_name
    kernel /dev/fs/ext2@wd0/boot/$kernel_name
    args console=tty loglevel=0 quiet $splash locales=zh_CN.UTF-8 root=$rootuuid

title Lingmo Recovery
    initrd /dev/fs/ext2@wd0/boot/lingmo/$initrd_name
    kernel /dev/fs/ext2@wd0/boot/lingmo/$kernel_name
    args boot=live loglevel=0 components $splash toram locales=zh_CN.UTF-8 acpi_backlight=vendor live-media-path=recovery " >> $boot_path/boot.cfg

    echo "
menuentry 'Lingmo 15.5 SP3 Professional mips64el for Loongson 3A3000'{
linux \${prefix}/$kernel_name console=tty loglevel=0 quiet $splash locales=zh_CN.UTF-8  root=$rootuuid
initrd \${prefix}/$initrd_name
boot
}

menuentry 'Lingmo Recovery'{
linux \${prefix}/lingmo/$kernel_name console=tty loglevel=0  $splash toram locales=zh_CN.UTF-8 boot=live components acpi_backlight=vendor live-media-path=recovery
initrd \${prefix}/lingmo/$initrd_name
boot
}" >>  $boot_path/grub.cfg

    if [ "$boot_path" = "$TARGET" ];then
        sed -i "s|wd0/boot|wd0|g" $boot_path/boot.cfg
    fi

fi

# fix boot.cfg
#if grep -wqs $TARGET /proc/mounts ; then
#  sed -e 's@boot/@@g' -i $TARGET/boot.cfg
#fi

chroot /target /usr/sbin/update-initramfs -u -k all

return 0
